package org.jeecg.modules.geo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.geo.core.query.BimGeoQueryGenerator;
import org.jeecg.modules.geo.entity.BimGeoApi;
import org.jeecg.modules.geo.service.IBimGeoApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: bim_geo_api
 * @Author: jeecg-boot
 * @Date: 2022-12-05
 * @Version: V1.0
 */
@Api(tags = "接口配置")
@RestController
@RequestMapping("/geo/bimGeoApi")
@Slf4j
public class BimGeoApiController extends JeecgController<BimGeoApi, IBimGeoApiService> {
    @Autowired
    private IBimGeoApiService bimGeoApiService;

    /**
     * 分页列表查询
     *
     * @param bimGeoApi
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "接口配置-分页列表查询", notes = "接口配置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<BimGeoApi>> queryPageList(BimGeoApi bimGeoApi,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<BimGeoApi> queryWrapper = BimGeoQueryGenerator.initQueryWrapper(bimGeoApi, req.getParameterMap());
        Page<BimGeoApi> page = new Page<>(pageNo, pageSize);
        IPage<BimGeoApi> pageList = bimGeoApiService.pageList(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 应用下api列表查询
     *
     * @return
     */
    @ApiOperation(value = "应用下api列表查询", notes = "应用下api列表查询")
    @GetMapping(value = "/byAppList")
    public Result<?> queryApiList(@RequestParam(value = "appId") Integer appId) {
        List<BimGeoApi> bimGeoApis = bimGeoApiService.geoApiByApp(appId, null);
        return Result.OK(bimGeoApis);
    }

    /**
     * 添加
     *
     * @param bimGeoApi
     * @return
     */
    @AutoLog(value = "接口配置-添加")
    @ApiOperation(value = "接口配置-添加", notes = "接口配置-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Valid BimGeoApi bimGeoApi) {
        BimGeoApi bimGeoApiParam = bimGeoApiService.find(bimGeoApi.getAppId(), bimGeoApi.getPath());
        if (bimGeoApiParam != null) {
            return Result.error("添加失败，应用接口地址已存在！！！");
        }
        bimGeoApiService.add(bimGeoApi);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param bimGeoApi
     * @return
     */
    @AutoLog(value = "api-编辑")
    @ApiOperation(value = "接口配置-编辑", notes = "接口配置-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody BimGeoApi bimGeoApi) {
        bimGeoApiService.update(bimGeoApi);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "bim_geo_api-通过id删除")
    @ApiOperation(value = "接口配置-通过id删除", notes = "接口配置-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") Integer id) {
        bimGeoApiService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "bim_geo_api-批量删除")
    @ApiOperation(value = "bim_geo_api-批量删除", notes = "bim_geo_api-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        bimGeoApiService.removeByIds(list);
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "bim_geo_api-通过id查询", notes = "bim_geo_api-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<BimGeoApi> queryById(@RequestParam(name = "id") Integer id) {
        BimGeoApi bimGeoApi = bimGeoApiService.getById(id);
        if (bimGeoApi == null) {
            return Result.error("未找到对应数据", null);
        }
        return Result.OK(bimGeoApi);
    }


    /**
     * 导出excel
     *
     * @param request
     * @param bimGeoApi
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BimGeoApi bimGeoApi) {
        return super.exportXls(request, bimGeoApi, BimGeoApi.class, "bim_geo_api");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BimGeoApi.class);
    }

}
